package com.tuyue.police.weixin.handler;

import com.alibaba.fastjson.JSONObject;
import com.tuyue.police.util.Alicloudapi;
import com.tuyue.police.util.JsonUtils;
import com.tuyue.police.weixin.builder.TextBuilder;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {
        String content = "";
        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
            //TODO 可以选择将消息保存到本地
            if (wxMessage.getContent().equals("维度")) {
                content = "企业名称：维度安全技术服务（天津）有限公司\n" +
                        "工商执照注册号：91120222MA05Y6J12R";
            } else {
                String zineng = Alicloudapi.Zineng(wxMessage.getContent());
                JSONObject jsonObject = new JSONObject().parseObject(zineng);
                String status = jsonObject.getString("status");
                if (status.equals("0")) {
                    JSONObject result = jsonObject.getJSONObject("result");
                    content = result.getString("content");
                }
            }
        } else {
            content = JsonUtils.toJson(wxMessage);
        }
        return new TextBuilder().build(content, wxMessage, weixinService);

    }

}
